package websocket

import (
	"context"
	"github.com/gogf/gf/v2/os/gctx"
	"github.com/gogf/gf/v2/os/gtime"
)

type (
	wContext struct {
		loggerKey string
	}
	ContextLogger struct {
		ClientTraceId string      `json:"client_trace_id"`
		TraceId       string      `json:"trace_id"`
		UpTime        *gtime.Time `json:"up_time"`
		FinishTime    *gtime.Time `json:"finish_time"`
		RunTime       string      `json:"run_time"`
		Original      string      `json:"original"`
	}
)

var (
	insContext = wContext{
		loggerKey: "ContextLoggerKey",
	}
)

func Context() *wContext {
	return &insContext
}

func (w *wContext) InitLogger(logger ContextLogger, c ...context.Context) context.Context {
	var ctx context.Context
	if len(c) > 0 {
		ctx = c[0]
	} else {
		ctx = gctx.New()
	}
	logger.TraceId = gctx.CtxId(ctx)
	ctx = context.WithValue(ctx, w.loggerKey, logger)
	return ctx
}

func (w *wContext) GetLogger(ctx context.Context) ContextLogger {
	return ctx.Value(w.loggerKey).(ContextLogger)
}
